﻿/**
 * 	Реализация Роутеров под NODEJS для курсового проекта
 *	автор: А.А.Семченко ст.гр.702301 ИСИТ
 * 	Минск 2012г
 */

var express = require('./mongo/lib/express/express.js')
 , http = require('http')
 , connect = require('./mongo/lib/node_modules/connect');


var mongo = require('./mongo/lib/mongodb');
	var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
	var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : mongo.Connection.DEFAULT_PORT;
	console.log("Connecting to " + host + ":" + port);
var ObjectID = require('./mongo/lib/mongodb').ObjectID;
	
	
var db = new mongo.Db('node-mongo-examples', new mongo.Server(host, port, {}), {});

db.open(function(err, db){
		
		Item = {
			size:0,
			vagon:[],
			vesgr:[],
			kodgr:[],
			esrsn:[],
			id:[],
			_id:[],
			
			add:function(vagon,vesgr,kodgr,esrsn,id,_id){
				this.vagon.push(vagon)
				this.vesgr.push(vesgr)
				this.kodgr.push(kodgr)
				this.esrsn.push(esrsn)
				this.id.push(id)
				this._id.push(_id)
				this.size++
			},
			clear:function(){
				this.vagon=[]
				this.vesgr=[]
				this.kodgr=[]
				this.esrsn=[]
				this.id=[]
				this._id=[]
				this.size=0
			}
		}
		
		var app = express.createServer();

		app.get('/', function(req, res){
		  res.send('Request:<br/>'+req.query.a+' + '+req.query.s);
		});
		
		app.get('/select/:id', function(req, res){
			db.collection('test', function(err, collection) {
				collection.find({_id:req.params.id+''},function(err, cursor) {
					cursor.toArray(function(err, items) {
						Item.clear();
						for (i=0;i<items.length;i++)
							Item.add(items[i].vagon,items[i].vesgr,items[i].kodgr,items[i].esrsn,items[i].id,items[i]._id)
						res.send(JSON.stringify(Item))	
					});
				});
			})
		});	
		
		app.get('/test/:id', function(req, res){
		
			db.collection('test', function(err, collection) {
				var idString = req.params.id;
				collection.find({_id:new ObjectID(idString)},function(err, cursor) { 
					cursor.toArray(function(err, items) {
						Item.clear();
						for (i=0;i<items.length;i++)
							Item.add(items[i].vagon,items[i].vesgr,items[i].kodgr,items[i].esrsn,items[i].id,items[i]._id)
						res.send(JSON.stringify(Item))	
					});
				});
			})
		});
		
		app.get('/select', function(req, res){
			db.collection('test', function(err, collection) {
				collection.find({},function(err, cursor) {
					cursor.toArray(function(err, items) {
						Item.clear();
						for (i=0;i<items.length;i++)
							Item.add(items[i].vagon,items[i].vesgr,items[i].kodgr,items[i].esrsn,items[i].id,items[i]._id)
						res.send(JSON.stringify(Item))	
					});
				});
			})
		});
		
		app.get('/insert/:id', function(req, res){
		//	{"vagon":88888888,"vesgr":75,"kodgr":12345,"esrsn":150000}

			res.send('Запись в БД'+req.params.id);
				obj = JSON.parse(req.params.id)
				db.collection('test', function(err, collection) {
					collection.insert({vagon:obj.vagon, vesgr:obj.vesgr, kodgr:obj.kodgr, esrsn:obj.esrsn,id:0});
				});
		});
		
		/*
		app.get('/delete/all', function(req, res){
			db.collection('test', function(err, collection) {
				collection.remove({}, function(err, collection) {
					res.redirect('/select');					
				})
				
			});
		});	
		*/
		app.get('/delete/:id', function(req, res){
			db.collection('test', function(err, collection) {
			var idString = req.params.id;
				collection.remove({_id:new ObjectID(req.params.id)}, function(err, collection) {
					res.send('Запись с ID='+req.params.id+' удалена')	
				})
				
			});
		});
	
		app.listen(3000);
		console.log('listening on port 3000');
})